Overview
Template sync is the process of synchronizing agent workspace files (TOOLS.md, SOUL.md, IDENTITY.md, etc.) from Mission Control to the OpenClaw gateway. This ensures agents have the latest configuration and credentials. Primary Endpoint:POST /api/v1/gateways/{gateway_id}/templates/sync
Implementation: backend/app/services/openclaw/admin_service.py:489-517
When to Sync Templates
Run template sync when:- First-time setup — Initial gateway configuration
- Agent credentials compromised — Use
rotate_tokens=true - Template updates — After modifying SOUL.md, IDENTITY.md, or TOOLS.md templates
- Agents deleted from gateway config — Use
rotate_tokens=trueto recreate entries - Configuration drift — Restore expected workspace state
Template Sync Parameters
All parameters are passed as query strings.| Parameter | Type | Default | Description |
|---|---|---|---|
include_main | Boolean | true | Include the gateway-main agent in sync |
lead_only | Boolean | false | Sync only board lead agents (skip workers) |
reset_sessions | Boolean | false | Force reset of agent sessions |
rotate_tokens | Boolean | false | Generate new authentication tokens for all agents |
force_bootstrap | Boolean | false | Force re-creation of BOOTSTRAP.md |
overwrite | Boolean | false | Overwrite existing workspace files |
board_id | UUID | null | Limit sync to agents on a specific board |
backend/app/services/openclaw/session_service.py (GatewayTemplateSyncQuery)
Sync Workflow
The template sync process executes the following steps:1. Ensure Gateway-Main Agent Exists
Implementation:backend/app/services/openclaw/admin_service.py:428-442
- Creates or updates the gateway-main agent record in the database
- Ensures the gateway config contains the main agent entry
- Provisions the main agent if missing or changed
2. Collect Target Agents
Implementation:backend/app/services/openclaw/provisioning_db.py
Filters agents based on parameters:
- If
board_idis specified, only agents on that board - If
lead_only=true, onlyis_board_lead=trueagents - If
include_main=false, exclude gateway-main agent
3. Read Existing Tokens
Implementation:backend/app/services/openclaw/provisioning.py:550-560
For each agent:
- Attempt to read the existing
TOOLS.mdviaagents.files.getRPC - Extract
AUTH_TOKENfrom the file content - If read fails and
rotate_tokens=false, log error and skip agent
4. Render Templates
Implementation:backend/app/services/openclaw/provisioning.py:562-571
For each agent, render workspace files using Jinja2 templates:
Template Location: backend/templates/
Board Lead Files:
TOOLS.md(fromBOARD_TOOLS.md.j2)SOUL.md(fromBOARD_SOUL.md.j2)IDENTITY.md(fromBOARD_IDENTITY.md.j2)HEARTBEAT.md(fromBOARD_HEARTBEAT.md.j2)AGENTS.md(fromBOARD_AGENTS.md.j2)ROLES.md(fromBOARD_ROLES.md.j2)BOOTSTRAP.md(from template)USER.md(from template)MEMORY.md(from template)
TOOLS.mdSOUL.mdIDENTITY.mdHEARTBEAT.mdBOOTSTRAP.mdUSER.mdMEMORY.md
TOOLS.md(fromGATEWAY_MAIN_TOOLS.md.j2)SOUL.md(fromGATEWAY_MAIN_SOUL.md.j2)IDENTITY.md(from template)USER.md(from template)
backend/app/services/openclaw/provisioning.py:306-351
5. Write Files to Gateway
Implementation:backend/app/services/openclaw/provisioning.py:573-574
For each file:
- Call
agents.files.setRPC to write content to agent workspace - Skip preserved files during updates unless
overwrite=true
USER.mdMEMORY.mdLEARNINGS.md
backend/app/services/openclaw/constants.py (PRESERVE_AGENT_EDITABLE_FILES)
6. Update Agent Heartbeat Config
Implementation:backend/app/services/openclaw/provisioning.py:604-619
Patch the gateway’s openclaw.json to update:
- Agent workspace path
- Heartbeat configuration (interval, triggers, notifications)
config.patch RPC to atomically update the config.
7. Reset Sessions (Optional)
Implementation:backend/app/services/openclaw/provisioning.py:1097-1102
If reset_sessions=true:
- Call
sessions.resetRPC for each agent session key - Clears agent memory and restarts conversation
Command Examples
Standard Sync
Sync all agents, preserving existing tokens:First-Time Setup or Token Rotation
Generate new tokens and overwrite all files:- Initial gateway setup
- Gateway config was manually edited and
mc-*agents removed - Tokens were compromised or lost
Sync Only Lead Agents
Update only board lead agents (useful for large deployments):Sync Specific Board
Limit sync to agents on a single board:Force Reset Sessions
Reset agent memory and restart conversations:Exclude Gateway-Main Agent
Sync only board agents:Response Structure
Success Response:backend/app/schemas/gateways.py (GatewayTemplatesSyncResult)
Common Errors
”unable to read AUTH_TOKEN from TOOLS.md”
Cause: The agent entry doesn’t exist in the gateway’sopenclaw.json, so the gateway cannot serve the TOOLS.md file.
Solution:
”missing scope: operator.read”
Cause: Gateway authentication is not configured correctly. Solution: Ensure the gateway’s~/.openclaw/openclaw.json contains:
”Gateway rejected required lead workspace files as unsupported”
Cause: The gateway version doesn’t support required lead workspace files (e.g.,AGENTS.md, ROLES.md).
Solution: Upgrade the gateway to version 2026.02.9 or later.
Version Check: The minimum version is defined in backend/.env as GATEWAY_MIN_VERSION=2026.02.9.
Token Workflow
Token Generation
Implementation:backend/app/services/openclaw/db_agent_state.py (mint_agent_token)
- Generate 32-byte random token using
secrets.token_urlsafe(32) - Hash token with
bcrypt(cost factor 12) - Store hash in
agents.agent_token_hash - Return raw token for template rendering
Token Storage
Tokens are stored in two places:- Database: Hashed in
agents.agent_token_hashfor authentication - Gateway Workspace: Plain text in agent’s
TOOLS.mdfile
Template Development
To modify templates:- Edit files in
backend/templates/ - Use Jinja2 syntax for variables:
{{ variable_name }} - Test template rendering locally
- Run template sync to deploy changes